﻿#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>

typedef struct People
{
	char name[20];
	int age;
	char sex[10];
	char phone[12];
	char home[30];
}people;

typedef struct Txl
{
	people peoples[100];
	int sz;
}txl;

int caidan();

void chushihua(txl* com);

void add(txl* com);

void print(txl* com);

int find(txl* com);

void show(txl* com, int n);

void del(txl* com);

void gai(txl* com);

void paixu(txl* com);

int main()
{
	txl com;
	chushihua(&com);
	int input;
	do
	{
		input = caidan();
		switch (input)
		{
		case 1:
			add(&com);
			break;
		case 2:
			del(&com);
			break;
		case 3:
		{
			int xiabiao = find(&com);
			if (xiabiao == -1)
			{
				printf("未找到该联系人\n");
			}
			else
			{
				printf("找到了，该联系人信息如下：\n");
				show(&com, xiabiao);
			}
		}
		break;
		case 4:
			gai(&com);
			break;
		case 5:
			paixu(&com);
			break;
		case 6:
			print(&com);
			break;
		case 0:
			printf("通讯录成功退出\n");
			break;
		default:
			printf("输入不合法，请重新输入\n");
			break;
		}
	} while (input);
	return 0;
}

int caidan()
{
	int input = 0;
	printf("****************************************\n");
	printf("***           通讯录功能表           ***\n");
	printf("***   1.增加联系人    2.删除联系人   ***\n");
	printf("***   3.查找联系人    4.更改联系人   ***\n");
	printf("***   5.通讯录排序    6.打印通讯录   ***\n");
	printf("***   0.退出                         ***\n");
	printf("****************************************\n");
	printf("请选择功能:");
	scanf("%d", &input);
	return input;
}

void chushihua(txl* com)
{
	memset(com, 0, sizeof(txl));
	return;
}

void add(txl* com)
{
	assert(com);
	if (com->sz == 100)
	{
		printf("联系人已满，添加失败\n");
		return;
	}
	else
	{
		printf("联系人相关信息输入：\n");
		printf("请输入姓名：");
		scanf("%s", (com->peoples[com->sz].name));
		printf("请输入年龄：");
		scanf("%d", &com->peoples[com->sz].age);
		printf("请输入性别：");
		scanf("%s", com->peoples[com->sz].sex);
		printf("请输入电话：");
		scanf("%s", com->peoples[com->sz].phone);
		printf("请输入地址：");
		scanf("%s", com->peoples[com->sz].home);
		/*printf("请依次输入联系人的姓名，年龄，性别，电话，地址（中间用空格隔开）:");
		scanf("%s%d%s%s%s", com->peoples[com->sz].name
			, &com->peoples[com->sz].age
			, com->peoples[com->sz].sex
			, com->peoples[com->sz].phone
			, com->peoples[com->sz].home);*/
	}
	com->sz++;
	printf("添加成功\n");
	return;
}

void print(txl* com)
{
	printf("通讯录信息展示：\n");
	printf("%-20s\t%-5s\t%-10s\t%-12s\t%-30s\n", "姓名", "年龄", "性别", "电话", "地址");
	int i = 0;
	for (i = 0; i < com->sz; i++)
	{
		printf("%-20s\t%-5d\t%-10s\t%-12s\t%-30s\n", com->peoples[i].name
			, com->peoples[i].age
			, com->peoples[i].sex
			, com->peoples[i].phone
			, com->peoples[i].home
		);
	}
	return;
}

int find(txl* com)
{
	assert(com);
	if (com->sz == 0)
	{
		printf("通讯录为空，查找失败\n");
		return -1;
	}
	else
	{
		printf("请输入需要查找联系人的姓名：");
		char name[20] = { 0 };
		scanf("%s", name);
		int i = 0;
		for (i = 0; i < com->sz; i++)
		{
			if (strcmp(name, com->peoples[i].name) == 0)
			{
				return i;
			}
		}
	}
	return -1;
}

void show(txl* com, int n)
{
	printf("%-20s\t%-5s\t%-10s\t%-12s\t%-30s\n", "姓名", "年龄", "性别", "电话", "地址");
	printf("%-20s\t%-5d\t%-10s\t%-12s\t%-30s\n", com->peoples[n].name
		, com->peoples[n].age
		, com->peoples[n].sex
		, com->peoples[n].phone
		, com->peoples[n].home
	);
	return;
}

void del(txl* com)
{
	int xiabiao = find(com);
	if (xiabiao == -1)
	{
		printf("该联系人不存在\n");
	}
	else
	{
		int i = xiabiao;
		for (i = xiabiao; i < com->sz - 1; i++)
		{
			com->peoples[i] = com->peoples[i + 1];
		}
		com->sz--;
		printf("删除成功\n");
		return;
	}
}

void gai(txl* com)
{
	int xiabiao = find(com);
	if (xiabiao == -1)
	{
		printf("该联系人不存在\n");
	}
	else
	{
		printf("联系人相关信息输入：\n");
		printf("请输入姓名：");
		scanf("%s", (com->peoples[xiabiao].name));
		printf("请输入年龄：");
		scanf("%d", &com->peoples[xiabiao].age);
		printf("请输入性别：");
		scanf("%s", com->peoples[xiabiao].sex);
		printf("请输入电话：");
		scanf("%s", com->peoples[xiabiao].phone);
		printf("请输入地址：");
		scanf("%s", com->peoples[xiabiao].home);

		printf("更改成功\n");
		return;
	}
}

int suanfa(const void* e1, const void* e2)
{
	return strcmp(((people*)e1)->name, ((people*)e2)->name);
}

void paixu(txl* com)
{
	qsort(com->peoples, com->sz, sizeof(people), suanfa);
	printf("排序成功\n");
}